package ch.zbw.pra.sysventory.ui.gui.menu;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ch.zbw.pra.sysventory.SysventoryApp;
import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.domain.model.Computer;
import ch.zbw.pra.sysventory.ui.MainFrame.PanelEnum;
import ch.zbw.pra.sysventory.ui.gui.panels.ueberpanel.UeberPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class MenuElements extends JMenuBar {

	private static final long serialVersionUID = -4331841414637560621L;

	private JMenu datei;
	private JMenu tools;
	private JMenu ansicht;
	private JMenu hilfe;

	private JMenuItem einstellungenMenuItem;
	private JMenuItem beendenMenuItem;
	private JMenuItem scannenMenuItem;
	private JMenuItem uebersichtMenuItem;
	private JMenuItem statistikMenuItem;
	private JMenuItem ueberMenuItem;
	// private JMenuItem hilfeMenuItem;

	private JPanel cards;

	public MenuElements(JPanel cards) {
		super();
		this.cards = cards;
		this.datei = new JMenu("Datei");
		this.tools = new JMenu("Tools");
		this.ansicht = new JMenu("Ansicht");
		this.hilfe = new JMenu("Hilfe");

		this.einstellungenMenuItem = new JMenuItem("Einstellungen");
		this.einstellungenMenuItem.addActionListener(new ChangePanelActionListener(
				PanelEnum.SETTINGS, cards));
		this.beendenMenuItem = new JMenuItem("Beenden");
		this.beendenMenuItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				SysventoryApp.saveBeforeClose();
				System.exit(0);
			}
		});
		if (DomainController.getInstance().checkHasInventorRights()) {
			this.scannenMenuItem = new JMenuItem("Scannen");

			this.scannenMenuItem.addActionListener(new ChangePanelActionListener(PanelEnum.SCAN,
					cards));
		}
		this.uebersichtMenuItem = new JMenuItem("\u00DCbersicht");
		this.uebersichtMenuItem.addActionListener(new ChangePanelActionListener(PanelEnum.SEARCH,
				cards));
		this.statistikMenuItem = new JMenuItem("Statistik");
		this.statistikMenuItem.addActionListener(new ChangePanelActionListener(PanelEnum.STATISTIC,
				cards));
		this.ueberMenuItem = new JMenuItem("\u00DCber");
		this.ueberMenuItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JDialog ueberUns = new JDialog();
				ueberUns.add(new UeberPanel(), BorderLayout.CENTER);
				ueberUns.setSize(500, 300);
				ueberUns.setVisible(true);
				try {
					ueberUns.setIconImage(new ImageIcon(new FileGenerator().getFile(
							FileType.ICON_PGS).getAbsolutePath()).getImage());
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					System.out.println("Bild laden fehlgeschlagen");
				}
				GuiLayout.centerWindow(ueberUns);
				ueberUns.setResizable(false);
				ueberUns.setAlwaysOnTop(true);
			}
		});
		// this.hilfeMenuItem = new JMenuItem("Hilfe");
		// hilfeMenuItem.addActionListener(this);

		addMenusToMenuBar();
		addMenuItemToMenues();
	}

	public void addMenusToMenuBar() {
		this.add(this.datei);
		if (DomainController.getInstance().checkHasInventorRights()) {
			this.add(this.tools);
		}
		this.add(this.ansicht);
		this.add(this.hilfe);
	}

	public void addMenuItemToMenues() {
		this.datei.add(this.einstellungenMenuItem);
		this.datei.add(this.beendenMenuItem);
		if (DomainController.getInstance().checkHasInventorRights()) {
			this.tools.add(this.scannenMenuItem);
		}
		this.ansicht.add(this.uebersichtMenuItem);
		this.ansicht.add(this.statistikMenuItem);
		this.hilfe.add(this.ueberMenuItem);
		// this.hilfe.add(this.hilfeMenuItem);
	}

	public void callDetailView(List<Computer> computer) {
		new ChangePanelActionListener(PanelEnum.DETAIL, this.cards);
	}

	private class ChangePanelActionListener implements ActionListener {
		private PanelEnum panelEnum;
		private JPanel cards;

		public ChangePanelActionListener(PanelEnum panelName, JPanel cards) {
			this.panelEnum = panelName;
			this.cards = cards;
		}

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			CardLayout layout = (CardLayout) this.cards.getLayout();
			layout.show(this.cards, this.panelEnum.toString());
		}
	}
}
